package com.wjy.simple.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.wjy.sfhcore.engine.BaseAsyncTaskLoader;
import com.wjy.sfhcore.ui.fragment.LoadFragment;
import com.wjy.simple.R;

/**
 * Created by wangjingyuan on 15/7/31.
 */
public class LoadDemoFragment extends LoadFragment<Object> {
    protected View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_load_demo_fragment, container, false);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mToolBar.setTitle("需要网络");
        changeViewState(loadResult);
        if (isFirstLoad) {
            isFirstLoad = false;
            loadData();
        }
    }


    @Override
    public void menuListener(MenuItem item) {

    }

    @Override
    public void initBackButton() {
        baseActivity.finish();
    }

    @Override
    public boolean isUseToolBar() {
        return true;
    }

    @Override
    protected void loadFinished(int action, Object result) {
        mProgressBar.dismiss();
        if (result == null) {
            changeViewState(LoadResult.LOAD_FAIL);
        } else {
            changeViewState(LoadResult.LOAD_SUCCESS);
            refreshView();
        }
    }


    @Override
    public Loader<Object> onCreateLoader(int action, Bundle bundle) {
        return new BaseAsyncTaskLoader<Object>(baseActivity) {
            @Override
            public Object loadInBackground() {
                SystemClock.sleep(2000);
                return new Object();
            }
        };
    }

    private void refreshView() {
        SimpleDraweeView imageView = (SimpleDraweeView) rootView.findViewById(R.id.imageView);
        ImageRequest imageRequest =
                ImageRequestBuilder.newBuilderWithSource(Uri.parse("http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg"))
                        .build();
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequest)
                .setOldController(imageView.getController())
                .setAutoPlayAnimations(true)
                .build();
        imageView.setController(draweeController);
    }
    @Override
    public boolean isShowBackButton() {
        return false;
    }
}
